package com.demo0215.controller;

import com.demo0215.entity.Order;
import com.demo0215.entity.OrderVo;
import com.demo0215.repository.OrderRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RefreshScope
@RequestMapping("/order")
public class OrderController {
    @Autowired
    private OrderRepository orderRepository;
    @PostMapping("/save")
    public void save(@RequestBody Order order){
        orderRepository.save(order);
    }

    @GetMapping("/findAllByUid/{index}/{limit}/{uid}")
    public OrderVo findAll(@PathVariable("index") int index, @PathVariable("limit") int limit, @PathVariable("uid") long uid){
        List<Order> orders = orderRepository.findAllByUid(index,limit, uid);
        OrderVo orderVo = new OrderVo(200,"OK",orderRepository.countByUid(uid),orders);
        return orderVo;
    }

    @GetMapping("/countByUid/{uid}")
    public int countByUid(@PathVariable("uid") long uid){
       return orderRepository.countByUid(uid);
    }
}
